package org.ysling.litemall.db.service;
// Copyright (c) [ysling] [927069313@qq.com]
// [litemall-plus] is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
//             http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
import com.github.pagehelper.PageHelper;
import org.ysling.litemall.db.dao.LitemallUserMapper;
import org.ysling.litemall.db.domain.LitemallUser;
import org.ysling.litemall.db.example.LitemallUserExample;
import org.ysling.litemall.db.vomain.UserVo;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
@CacheConfig(cacheNames = "user")
public class LitemallUserService {

    @Resource
    private LitemallUserMapper userMapper;

    @Cacheable(sync = true)
    public LitemallUser findById(Integer userId) {
        return userMapper.selectByPrimaryKey(userId);
    }

    @Cacheable(sync = true)
    public UserVo findUserVoById(Integer userId) {
        LitemallUser user = userMapper.selectByPrimaryKey(userId);
        UserVo userVo = new UserVo();
        if (user != null) {
            userVo.setNickname(user.getNickname());
            userVo.setAvatar(user.getAvatar());
            return userVo;
        }
        userVo.setNickname("匿名用户");
        userVo.setAvatar("https://ysling-1313283468.cos.ap-chongqing.myqcloud.com/PUANDY13U7N3VQRRA0F8.jpg");
        return userVo;
    }

    @Cacheable(sync = true)
    public LitemallUser queryByOid(String openId) {
        LitemallUserExample example = new LitemallUserExample();
        example.or().andWeixinOpenidEqualTo(openId).andDeletedEqualTo(false);
        return userMapper.selectOneByExample(example);
    }

    @Cacheable(sync = true)
    public List<LitemallUser> querySelective(Integer userId, Byte level, String mobile, Integer page, Integer size, String sort, String order) {
        LitemallUserExample example = new LitemallUserExample();
        LitemallUserExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);

        if (userId != null){
            criteria.andIdEqualTo(userId);
        }
        if (level != null){
            criteria.andUserLevelEqualTo(level);
        }
        if (StringUtils.hasText(mobile)) {
            criteria.andMobileLike("%" + mobile + "%");
        }

        if (StringUtils.hasText(sort) && StringUtils.hasText(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, size);
        return userMapper.selectByExample(example);
    }

    @Cacheable(sync = true)
    public int count() {
        LitemallUserExample example = new LitemallUserExample();
        example.or().andDeletedEqualTo(false);
        return (int) userMapper.countByExample(example);
    }

    @Cacheable(sync = true)
    public List<LitemallUser> queryByUsername(String username) {
        LitemallUserExample example = new LitemallUserExample();
        example.or().andUsernameEqualTo(username).andDeletedEqualTo(false);
        return userMapper.selectByExample(example);
    }

    @Cacheable(sync = true)
    public boolean checkByUsername(String username) {
        LitemallUserExample example = new LitemallUserExample();
        example.or().andUsernameEqualTo(username).andDeletedEqualTo(false);
        return userMapper.countByExample(example) != 0;
    }

    @Cacheable(sync = true)
    public List<LitemallUser> queryByMobile(String mobile) {
        LitemallUserExample example = new LitemallUserExample();
        example.or().andMobileEqualTo(mobile).andDeletedEqualTo(false);
        return userMapper.selectByExample(example);
    }

    @Cacheable(sync = true)
    public List<LitemallUser> queryByOpenid(String openid) {
        LitemallUserExample example = new LitemallUserExample();
        example.or().andWeixinOpenidEqualTo(openid).andDeletedEqualTo(false);
        return userMapper.selectByExample(example);
    }

    @CacheEvict(allEntries = true)
    public void add(LitemallUser user) {
        user.setAddTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        userMapper.insertSelective(user);
    }

    @CacheEvict(allEntries = true)
    public int updateById(LitemallUser user) {
        user.setUpdateTime(LocalDateTime.now());
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @CacheEvict(allEntries = true)
    public int updateVersionSelective(LitemallUser user) {
        user.setUpdateTime(LocalDateTime.now());
        return userMapper.updateWithVersionByPrimaryKeySelective(user.getVersion(), user);
    }

    @CacheEvict(allEntries = true)
    public void deleteById(Integer id) {
        userMapper.logicalDeleteByPrimaryKey(id);
    }
}
